package cn.bblocks.cacheTest.suport;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.CreatedBy;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedBy;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import com.fasterxml.jackson.annotation.JsonIgnore;

import javax.persistence.Column;
import javax.persistence.EntityListeners;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
import javax.persistence.Version;

import java.util.Date;

/**
 * @author madfrog
 * @version 1.0 2019-02-21 13:59
 **/
@MappedSuperclass
@Data
@AllArgsConstructor
@NoArgsConstructor
@EntityListeners(AuditingEntityListener.class)
public class BaseDomain {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;
    
    @Column(updatable=false)
    @CreatedDate
    private Date createTime ;//@CreatedDate 单表只能出现一次
    
    @LastModifiedDate
    @JsonIgnore
    private Date modifyTime;
    
    @Column(updatable=false,insertable = true ,columnDefinition = "varchar(32) comment '创建自'")
    @CreatedBy
    @JsonIgnore
    private String createBy;

    @LastModifiedBy
    @Column(columnDefinition = "varchar(32) comment '最后修改人'")
    @JsonIgnore
    private String lastModifiedBy;
    
    
    /**
     * 切记更新完后，不能得到最新版本，对于缓存来说 1、清理出去，2、更新成功主动把 version+1
     */
    @Version
	 @Column(name = "version", columnDefinition="int default 0")
	 private Integer version;
    
    public boolean equals(Object obj)  
    {  
    	//ID一直即可认为是统一对象
    	if(obj instanceof BaseDomain)
    		return this.getId().equals(((BaseDomain)obj).getId());  
    	else
    		return false;
    }
    
}
